{extend name="layout/iframe" /}

{block name="content"}
<div class="layui-card-body layui-layout-admin" style="padding: 15px;">
    <form class="layui-form" action="" lay-filter="component-form-group">
        <table id="dataTable" lay-filter="dataTable"></table>

        <div class="layui-form-item layui-row layui-col-xs12">
            <div class="layui-input-block">
                <div class="layui-footer">
                    <button type="button" class="layui-btn layui-btn-primary close-btn">添加商品</button>
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="layComplete">完成</button>
                </div>
            </div>
        </div>
    </form>
</div>

<input type="hidden" class="datalist_url" value="{:url('admin/productprice/setproductprice')}">
{/block}

{block name="pagescript"}
<script type="text/javascript">
    layui.use(['form', 'layer', 'table'], function(){

        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery,
            table = layui.table;

        var datalist_url = $('.datalist_url').val();


        datalist_url += '?product_ids=' + $(parent.window.frames[0].document).find('.product_ids').val();
        datalist_url += '&product_price_id=' + $(parent.window.frames[0].document).find('input[name=id]').val();
        // Table 列表
        var tableIns = table.render({
            elem: '#dataTable',
            url : datalist_url,
            page : true,
            cellMinWidth : 95,
            //height : "full-100",
            limit : 10,
            limits : [10, 20, 50, 100],
            id : "tableList",
            cols : [[
                {field: 'name', title: '商品名称'},
                {field: 'bar_code', title: '条码', align:'center'},
                {field: 'purchase_price', title: '进货价', align:'center', templet:function(data){
                    return data.purchase_price / 100;
                }},
                {field: 'price', title: '售价', align:'center', templet:function(data){
                    return data.price / 100;
                }},
                {field: 'wholesale_price', title: '批发价', align:'center', templet:function(data){
                    return data.wholesale_price / 100;
                }},
                {field: 'new_price', title: '新售价', align:'center', edit: 'text'},
                {field: 'new_wholesale_price', title: '新批发价', align:'center', edit: 'text'},
                /*{field: 'new_member_integral', title: '新会员积分', align:'center', edit: 'text'},*/
                {title: '操作', fixed:"right", align:"center", templet:function(data){
                    var html = '';
                    html += '<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>';

                    return html;
                }}
            ]],
        });

        // 列表操作
        table.on('tool(dataTable)', function(obj){
            var layEvent = obj.event,
                data = obj.data;

            // 删除
            if (layEvent === 'del') {
                layer.confirm('确定删除此数据？',{icon:3, title:'提示信息'},function(index){
                    // 删除商品
                    var product_ids_elm = $(parent.window.frames[0].document).find('.product_ids');
                    var product_ids = product_ids_elm.val();
                    var arr = product_ids.split(',');
                    var new_product_ids = '';
                    for(var i = 0; i < arr.length; i++){
                        if(arr[i] != obj.data.id){
                            new_product_ids += ',' + arr[i];
                        }
                    }
                    product_ids_elm.val(new_product_ids.substr(1));

                    layer.msg('已删除', {icon: 1, time: 1000});
                    //tableIns.reload();
                    table.reload("tableList", {
                        where: {
                            product_ids: new_product_ids
                        }
                    });
                    layer.close(index);

                    $(parent.document).find("iframe")[1].contentWindow.location.reload();
                });
            }
        });

        // 完成
        form.on("submit(layComplete)", function(data){
            //var checkStatus = table.checkStatus('tableList');
            //var data = checkStatus.data;
            //var data = tableIns.config.data;
            var data = table.cache.tableList;
            //console.log(data);
            if (data.length > 0) {
                var product_price_item_elm = $(parent.window.frames[0].document).find('.product_price_item');
                product_price_item_elm.empty();

                for (var i=0; i<data.length; i++) {
                    // 新售价
                    var new_price = data[i].new_price;
                    if (!new_price) {
                        layer.msg("请输入新售价");
                        return false;
                    } else {
                        if(!/^(([1-9]\d*)|\d)(\.\d{1,2})?$/.test(new_price)){
                            layer.msg("新售价格式不正确，应为正整数，或者小数点后只有两位的小数");
                            return false;
                        }
                        if(new_price <= 0){
                            layer.msg("新售价应大于0");
                            return false;
                        }
                    }
                    // 新批发价
                    var new_wholesale_price = data[i].new_wholesale_price;
                    if (!new_wholesale_price) {
                        layer.msg("请输入新批发价");
                        return false;
                    } else {
                        if(!/^(([1-9]\d*)|\d)(\.\d{1,2})?$/.test(new_wholesale_price)){
                            layer.msg("新批发价格式不正确，应为正整数，或者小数点后只有两位的小数");
                            return false;
                        }
                        if(new_wholesale_price <= 0){
                            layer.msg("新批发价应大于0");
                            return false;
                        }
                    }
                    // 新会员积分
                    /*var new_member_integral = data[i].new_member_integral;
                    if (!new_member_integral) {
                        layer.msg("请输入新会员积分");
                        return false;
                    } else {
                        if(!/^\+?[1-9][0-9]*$/.test(new_member_integral)){
                            layer.msg("新会员积分格式不正确，应为大于0的正整数");
                            return false;
                        }
                    }*/

                    var html = '';
                    html += '<input type="hidden" class="layui-input" name="product_id[]" value="'+data[i].id+'">';
                    html += '<input type="hidden" class="layui-input" name="new_price[]" value="'+new_price+'">';
                    html += '<input type="hidden" class="layui-input" name="new_wholesale_price[]" value="'+new_wholesale_price+'">';
                    //html += '<input type="hidden" class="layui-input" name="new_member_integral[]" value="'+new_member_integral+'">';
                    product_price_item_elm.append(html);
                }

                var indexLay = parent.layer.getFrameIndex(window.name); // 先得到当前iframe层的索引
                parent.layer.close(indexLay);
                // 关闭商品iframe层
                var indexLay2 = parent.layer.getFrameIndex($(parent.document).find("iframe")[1].contentWindow.name);
                parent.layer.close(indexLay2);
            } else {
                layer.msg("请添加商品数据");
            }
        });
    });
</script>
{/block}